﻿Array.prototype.remove = function (from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
}
Array.prototype.indexOf = function (substr, start) {
    var ta, rt, d = '\0';
    if (start != null) { ta = this.slice(start); rt = start; } else { ta = this; rt = 0; }
    var str = d + ta.join(d) + d, t = str.indexOf(d + substr + d);
    if (t == -1) return -1; rt += str.slice(0, t).replace(/[^\0]/g, '').length;
    return rt;
};
Array.prototype.lastIndexOf = function (substr, start) {
    var ta, rt, d = '\0';
    if (start != null) { ta = this.slice(start); rt = start; } else { ta = this; rt = 0; }
    ta = ta.reverse(); var str = d + ta.join(d) + d, t = str.indexOf(d + substr + d);
    if (t == -1) return -1; rt += str.slice(t).replace(/[^\0]/g, '').length - 2;
    return rt;
};
Array.prototype.replace = function (reg, rpby) {
    var ta = this.slice(0), d = '\0';
    var str = ta.join(d); str = str.replace(reg, rpby);
    return str.split(d);
};
Array.prototype.search = function (reg) {
    var ta = this.slice(0), d = '\0', str = d + ta.join(d) + d, regstr = reg.toString();
    reg = new RegExp(regstr.replace(/\/((.|\n)+)\/.*/g, '\\0$1\\0'), regstr.slice(regstr.lastIndexOf('/') + 1));
    t = str.search(reg); if (t == -1) return -1; return str.slice(0, t).replace(/[^\0]/g, '').length;
};

(function ($) {
 
    var ulclasslist = new Array();
    var itemtemplatelist = new Array();

    jQuery.registerListItem = function (ulclass,itemtemplate) {

         if (ulclass && itemtemplate) {
            ulclasslist.push(ulclass);
            itemtemplatelist.push(itemtemplate);
        }
        else if (ulclass && !itemtemplate) {
            var index = ulclasslist.indexOf(ulclass);
            return itemtemplatelist[index];
        }
        return this;
    };

    jQuery.removeregistedListItem = function (ulclass) {
         if (ulclass) {
           var index = ids.indexOf(attrid);
           if(index>=0)
           {
            ulclasslist.splice(index, 1); 
            itemtemplatelist.splice(index, 1); 
            }
        }
        return this;
    };
})(jQuery);



(function ($) {
    jQuery.fn.addListItem = function (options) {
        var settings = jQuery.extend({
            lidata: null,
            displaytext:null
        }, options);
        var $target = $(this);
        if ($target.is('ul')) {
            $target.css('list-style-type', 'none');
            var $templateitem;
            var $iteminstance;
            var classes = $(this).attr('class').split(' '); 
            for(var i=0;i<classes.length;i++){
               $templateitem = $.registerListItem(classes[i]);
               if($templateitem)
               break;
            }
            if($templateitem){
                var $iteminstance = $templateitem.clone().appendTo($target);
                if(settings.data) 
                   $iteminstance.data('lidata', data);

                if(settings.displaytext)
                   $iteminstance.find('div.display-text').text(settings.displaytext);
                if(settings.lidata){
                   $iteminstance.data('lidata',settings.lidata);
                   $iteminstance.removeClass('unsave').addClass('saved');
                   }
               
            }
            else
            throw Error('未发现匹配li');

        }
        return $iteminstance;
    };
})(jQuery);

